// pages/sales-return/sales-return.js
const apiurl = require('../../config').apiurl;
import { Pulibc } from '../../models/public.js'
const pulibc = new Pulibc()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    courierservices: [], // 快递
    goods: {}, // 商品
    orinfo: {}, // 用户信息
    shouhuocinf: {}, // 退货地址,
    cour_index:0,
    e_id:0, //快递ID
    e_name:'', // 快递名称
    invoice_no:'', // 快递单号
    reason: [{ id: 1, name: '请选择' }, { id: 2, name: '商品破损瑕疵' }],
    reason_id:0,
    reason_name:'请选择'
  },
  // 输入单号
  courierNumber(e){
    
    let invoice_no = e.detail.value
    this.setData({
      invoice_no
    })
  },
  // 退款原因
  reasonTypeChange(e){
    console.log(e)
    let index = e.detail.value;
    this.data.reason.forEach((item,idx)=>{
      if (idx == index){
        this.setData({
          reason_id:index,
          reason_name:item.name,

        })
      }
    })
  },
  // 选择快递
  bindBusinessTypeChange(e){
    console.log(e)
    let index = e.detail.value;
    this.data.courierservices.forEach((item, idx) => {
      if (idx == index) {
        this.setData({
          cour_index: index,
          e_name: item.e_name,
          e_id: item.e_id 
        })
        console.log(this.data.e_name)
        console.log(this.data.e_id)
        return;
      }
    })
  },

  /** 获取退款信息 */
  getOrderGoodsrefund(order_id, goods_id){
    let url = apiurl + '/order/order_goodsrefund';
    let key = wx.getStorageSync('key');
    wx.request({
      url: url,
      header: {
        'Content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      data: { order_id, goods_id,key},
      success:data=>{
        
        let order = data.data.datas.order
        console.log(order)
        let courierservices = order.courierservices // 快递
        let goods = order.goods // 商品
        let orinfo = order.orinfo // 用户信息
        let shouhuocinf = order.shouhuocinf // 退货地址
        this.setData({
          courierservices, 
          goods,
          orinfo,
          shouhuocinf,
          e_name: courierservices[0].e_name,
          e_id: courierservices[0].e_id
        })
      }
    })
    
  },
  /** 申请退款 */
  getOrderGrefund(e){
    let key = wx.getStorageSync('key')
    let url = apiurl + '/order/order_grefund'
    let order_id = this.data.order_id
    let goods_id = this.data.goods_id
    let invoice_no = this.data.invoice_no
    let e_id = this.data.e_id
    let express_name = this.data.e_name
    let reason = this.data.reason_name
    
    console.log(order_id)
    console.log(goods_id)
    console.log(invoice_no)
    console.log(e_id)
    console.log(express_name)
    if (invoice_no == ''){
      pulibc.showToast('请输入快递单号', 'none');
      return false;
    }
    if (reason == '请选择'){
      pulibc.showToast('请选择退货原因', 'none');
      return false;
    }
    wx.request({
      url: url,
      header: {
        'Content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      data: { key, order_id, goods_id, invoice_no, e_id, express_name,reason},
      success:data=>{
        console.log(data)
        if(data.data.code == 200){
          pulibc.showToast(data.data.info)

        }else{
          pulibc.showToast(data.data.datas.error,'none')
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    let goods_id = options.goods_id
    let order_id = options.order_id
    this.getOrderGoodsrefund(order_id, goods_id)
    this.setData({
      goods_id,
      order_id
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})